import 'package:casdon/common/utils/string_externtion.dart';
import 'package:casdon/core/extentions/string_validation.dart';
import 'package:casdon/modules/recipe/data/api/category_recipe_list_api.dart';

import 'package:casdon/modules/recipe/data/model/recipe_placeholder_data.dart';
import 'package:casdon/modules/recipe/ui/widgets/Recipe_card_item.dart';

class CategoryRecipeModel extends RecipeCardData {
  @override
  int get sort => 0;
  final String id;
  final String name;
  final String mainImg;
  final String view;
  final String likes;
  final String collection;
  final bool isControl;

  CategoryRecipeModel({
    required this.id,
    required this.name,
    required this.mainImg,
    required this.view,
    required this.likes,
    required this.collection,
    required this.isControl,
    required this.url,
  });

  @override
  final String? url; // 视频链接，

  @override
  bool get isSmart => isControl;

  @override
  bool get isVideo => (url ?? "").isNotEmpty;

  @override
  String get recipeId => id;
  @override
  String get imgUrl => mainImg.cropParams700;

  @override
  RecipeCardData placeholder() {
    throw UnimplementedError();
  }

  @override
  String get title => name;

  @override
  String get viewCount => view;

  @override
  String get count => "0";

  // 静态方法转换
  static CategoryRecipeModel fromApi(CategoryRecipe response) {
    String _pickImg() {
      final main = response.mainImg ?? '';
      if (main.split(',').first.isUrl()) return main.split(',').first;
      final imgs = response.imageUrls ?? '';
      if (imgs.split(',').first.isUrl()) return imgs.split(',').first;
      final cover = response.videoCoverUrls ?? '';
      if (cover.split(',').first.isUrl()) return cover.split(',').first;
      return '';
    }

    final pickedImg = _pickImg();
    return CategoryRecipeModel(
      id: response.id,
      name: response.name,
      mainImg: pickedImg.isEmpty ? recipePlaceHolderImgUrl : pickedImg,
      view: response.view ?? "0",
      likes: response.likes ?? "0",
      collection: response.collection ?? "0",
      isControl: (response.isControl ?? 0) == 0 ? false : true,

      url: response.videoUrls?.split(",").first,
    );
  }

  // 批量转换的静态方法
  static List<CategoryRecipeModel> listFromApi(List<CategoryRecipe> items) {
    return items.map(fromApi).toList();
  }
}
